package user

import (
	"context"

	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserVipLevelStatLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserVipLevelStatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserVipLevelStatLogic {
	return &UserVipLevelStatLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserVipLevelStatLogic) UserVipLevelStat() (resp *types.UserVipLevelStatResponse, err error) {
	stats, err := l.svcCtx.UserServiceRPC.UserVipLevelStat(l.ctx, nil)
	if err != nil {
		logc.Error(l.ctx, "UserVipLevelStat failed", err)
		return &types.UserVipLevelStatResponse{
			StatList: []*types.UserVipLevelStat{},
		}, nil
	}

	statList := make([]*types.UserVipLevelStat, len(stats.Data))
	for index, item := range stats.Data {
		statList[index] = &types.UserVipLevelStat{
			Id:       item.Id,
			VipLevel: item.VipLevel,
			Count:    item.Count,
		}
	}
	return &types.UserVipLevelStatResponse{
		StatList: statList,
	}, nil
}
